const Koa = require("koa");
const KoaBody = require("koa-body");
const router = require("../router/router.js");
const errorHandler = require("./errorHandler.js");
const cors = require("koa2-cors");
const static = require("koa-static");
const views = require("koa-views");
const app = new Koa();
const path = require("path");
//设置跨域
app.use(cors());
app.use(views(path.join(__dirname, "../", "views")));
app.use(static(path.join(__dirname, "../", "public")));
app.use(
  KoaBody({
    multipart: true,
    //strict: false, //启用不解析get,head,delete
    formidable: {
      keepExtensions: true,
      multipart: true,
    },
  })
);
//设置全局变量--网站根目录
/* app.use(async (ctx, next) => {
  let rootDir = path.join(__dirname, "../");
  ctx.state = Object.assign(ctx.state, { rootDir });
  await next();
}); */

app.use(router.routes());
app.use(router.allowedMethods());

app.on("error", errorHandler);
module.exports = app;
